//
//  ReweetedStatusView.swift
//  SinaWeibo
//
//  Created by gaoliang on 15/11/16.
//  Copyright © 2015年 gaoliang. All rights reserved.
//

import UIKit
import SnapKit

class ReweetedStatusView: UIView {
    
    // 转发微博底部约束 用来记录底部约束 后续根据是否有配图 来动态修改
    var bottomConstraints: Constraint?
    
    // 数据模型
    var retweetedStatus: Status? {
        didSet {
            retweetedLabe.text = retweetedStatus?.text
            
            // 移除底部约束
            self.bottomConstraints?.uninstall()
            if let picurls = retweetedStatus?.imageURLs where picurls.count > 0 {
                // 设置数据源
                pictureView.imageURLs = picurls
                // 显示配图视图
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    // 将约束条件转换成 约束类型
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                pictureView.hidden = true
                // 将约束类型转换成约束类型
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成 约束类型
                    self.bottomConstraints = make.bottom.equalTo(retweetedLabe.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    //MARK:3.重写父类的构造方法 调用设置 UI
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
    //MARK: 2.设置 UI
    private func setupUI() {
        addSubview(retweetedLabe)
        addSubview(pictureView)
        
        // 设置约束
        retweetedLabe.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(retweetedLabe.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(retweetedLabe.snp_left)

            
        }
        self.snp_makeConstraints { (make) -> Void in
            // 解除约束
           self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        
    }
    //MARK: 1. 懒加载所有子控件
    private lazy var retweetedLabe: UILabel = UILabel(title: "我是转发微博的", size: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    private lazy var pictureView: StatusPictureView = StatusPictureView()
}
